<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title.sync="title" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" class="input_text" :readonly="isView" />
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="form.account" class="input_text" :readonly="isView" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入新密码" maxlength="18" :show-password="true" class="input_text" :readonly="isView" />
      </el-form-item>
      <el-form-item label="手机号码" prop="cellphone">
        <el-input v-model="form.cellphone" class="input_text" :readonly="isView" />
      </el-form-item>
      <el-form-item label="所属客户" prop="customerId">
        <el-select v-model="form.customerId" placeholder="所属客户" :disabled="isView" style="width: 370px" @change="indexSelect($event)">
          <el-option v-for="(item,index) in customerItem" :key="index" :label="item.customerName" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-show="isView===false || isAdd===true" type="text" @click="cancel">取消</el-button>
      <el-button v-show="isAdd===false && isView===true" :loading="loading" type="primary" @click="cancel">返回</el-button>
      <el-button v-show="isView===false" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { add, edit } from '@/api/tCustomerUser'
import { isvalidPhone } from '@/utils/validate'

export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    isView: {
      type: Boolean,
      required: true
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入正确的11位手机号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      dialog: false,
      title: '',
      customerItem: [],
      form: {
        id: '',
        username: '',
        account: '',
        password: '',
        cellphone: '',
        customerId: '',
        customerName: '',
        creatTime: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        cellphone: [
          { required: true, trigger: 'blur', validator: validPhone }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        customerId: [
          { required: true, message: '请选择所属客户', trigger: 'blur' }
        ]
      }
    }
  },
  mounted: function() {
    this.tcustmerInfoAll()
    this.title = this.isAdd ? '新增' : (this.isView === true ? '详情' : '编辑')
  },
  methods: {
    tcustmerInfoAll() {
      this.getAllcustmer().then(res => {
        this.customerItem = res
        // 根据id再进行全部查询
      })
    },
    getAllcustmer() {
      return request({
        url: 'api/tCustomerInfoAll',
        method: 'get'
      })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.supThis.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        username: '',
        account: '',
        password: '',
        cellphone: '',
        customerId: '',
        customerName: '',
        creatTime: ''
      }
    }
  }
}

</script>

<style scoped>
.input_text {
  width: 370px;
}
</style>
